﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System;
using System.Collections.Generic;
namespace BeatingMole
{
    class CBusLoadingGame
    {
        #region "Variable"
        bool IsHelp = true;
        /// <summary>
        /// Thông tin Loading Game
        /// </summary>
        CInfoLoadingGame InfoLoadingGame;
        /// <summary>
        /// Quản lý Resource
        /// </summary>
        ContentManager _content;
        SpriteBatch _spriteBatch;
        GameTime _gameTime;
        /// <summary>
        /// Thời gian Loading Game
        /// </summary>
        int Delay;
        #endregion

        #region "Bus"
        /// <summary>
        /// Đối tượng CBusMap
        /// </summary>
        CBusMap BusMap;
        #endregion

        #region "Properties"
        #endregion

        #region "Constructor"
        public CBusLoadingGame(ContentManager content, SpriteBatch spriteBatch, GameTime gameTime)
        {
            _content = content;
            _spriteBatch = spriteBatch;
            _gameTime = gameTime;

            InfoLoadingGame = new CInfoLoadingGame(_content);

            BusMap = new CBusMap(content, spriteBatch, gameTime);

            MediaPlayer.Pause();
            MediaPlayer.Play(InfoLoadingGame.InfoLoadingSong);
        }
        #endregion

        #region "Methods"
        public void UpdateLoadingGame(GameTime gameTime)
        {
            if (IsHelp == false)
            {
                Delay += (int)gameTime.ElapsedGameTime.TotalMilliseconds;
                if (Delay >= InfoLoadingGame.DelayLoadingMap)
                {
                    if (CGlobalVariable.IsWin == false)
                    {
                        if (CGlobalVariable.IsPauseGame == false)
                        {                            
                            //Phương thức cập nhật
                            BusMap.Update(gameTime);
                            BusMap.UpdateMap(gameTime);
                            BusMap.UpdateButtonSettings(gameTime);
                            BusMap.UpdateButton(gameTime);
                            BusMap.UpdatePlayer(gameTime);
                            BusMap.UpdateWeapons(gameTime);
                            BusMap.UpdateMouseMove(gameTime);
                            BusMap.UpdateWin(gameTime);
                            if (CGlobalVariable.IsMute == true)
                            {
                                MediaPlayer.Volume = 1;
                            }
                        }                        
                    }
                }
                else
                {
                    //Update trong lúc Loading.
                }
            }
            else
            {
                CGlobalVariable.mouseStateCurrent = Mouse.GetState();
                if (CGlobalVariable.mouseStateCurrent.LeftButton == ButtonState.Pressed && CGlobalVariable.mouseStatePrevious.LeftButton == ButtonState.Released)
                {
                    if (new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1).Intersects(InfoLoadingGame.RecOKPlay))
                    {
                        IsHelp = false;
                    }
                }
                CGlobalVariable.mouseStatePrevious = CGlobalVariable.mouseStateCurrent;
            }
        }

        public void DrawLoadingGame(SpriteBatch spriteBatch)
        {
            if (IsHelp == false)
            {
                if (Delay >= InfoLoadingGame.DelayLoadingMap)
                {
                    BusMap.Draw(spriteBatch);
                    BusMap.DrawMap(spriteBatch);
                    BusMap.DrawTime(spriteBatch);
                    BusMap.DrawButton(spriteBatch);
                    BusMap.DrawButtonSettings(spriteBatch);
                    BusMap.DrawWeapons(spriteBatch);

                    BusMap.DrawPlayer(spriteBatch);
                    BusMap.DrawNamePlayer(spriteBatch);
                    BusMap.DrawScorePlayer(spriteBatch);
                    BusMap.DrawMouseMove(spriteBatch);
                    BusMap.DrawPauseGame(spriteBatch);
                    BusMap.DrawGameOver(spriteBatch);
                    BusMap.DrawWin(spriteBatch);
                }
                else
                {
                    int Percent = Convert.ToInt32((Delay / InfoLoadingGame.DelayLoadingMap) * 100);
                    spriteBatch.Draw(InfoLoadingGame.TextLoading, Vector2.Zero, Color.White);
                    spriteBatch.DrawString(InfoLoadingGame.SpriteFontTimeLeft, Percent.ToString() + "%", new Vector2(550, 420), Color.White);
                }
            }
            else
            {
                spriteBatch.Draw(InfoLoadingGame.LoadingHelp, Vector2.Zero, Color.White);
                spriteBatch.Draw(InfoLoadingGame.BtnOKPlay, new Vector2(575, 325), Color.White);
            }
        }

        public void ResumeGame(GameTime gameTime)
        {
            BusMap.UpdateResumeGame(gameTime);
        }
        #endregion
    }
}
